package com.example.viewpagermodule

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager

class MainActivity : AppCompatActivity() {
	//定义一个View集合
	private val viewList = ArrayList<View>()
	private lateinit var viewPager: ViewPager
	private var currentPostion: Int = 1

	private lateinit var linearLayout: LinearLayout
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)
		this.viewPager = findViewById(R.id.viewPager)
		//设置数据适配器
		initView()
		this.linearLayout = findViewById(R.id.ll_dots)
		initDots()
		this.viewPager.adapter = MyPagerAdapter(this.viewList)
		this.viewPager.adapter=object:FragmentPagerAdapter(supportFragmentManager,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
			override fun getCount(): Int {
				TODO("Not yet implemented")
			}

			override fun getItem(position: Int): Fragment {
				TODO("Not yet implemented")
			}

		}
		this.viewPager.currentItem = 1

		this.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
			override fun onPageScrolled(
				position: Int,
				positionOffset: Float,
				positionOffsetPixels: Int
			) {
//				TODO("Not yet implemented")

			}

			override fun onPageSelected(position: Int) {
//				TODO("Not yet implemented")
				if (position == 5) {
					currentPostion = 1
				} else if (position == 0) {
					currentPostion = 4
				} else {
					currentPostion = position
				}

				val dotPosition = position - 1
				//设置小点监听
				for (i in 0 until linearLayout.childCount) {
					if (i == dotPosition) {
						(linearLayout.getChildAt(i) as ImageView).setImageResource(R.drawable.image_select)
					} else {
						(linearLayout.getChildAt(i) as ImageView).setImageResource(R.drawable.image)

					}
				}
//				viewPager=ViewPager(this@MainActivity)
				val beginTransaction = supportFragmentManager.beginTransaction()
			}

			/**
			 * 状态有三种：
			 * 1. dragging:拖拽
			 * 2.idle:静止
			 * 3. setting:惯性过程
			 */
			override fun onPageScrollStateChanged(state: Int) {
//				TODO("Not yet implemented")
				if (state == ViewPager.SCROLL_STATE_IDLE) {
					viewPager.setCurrentItem(currentPostion, false)
				}
			}

		})
	}

	private fun initDots() {
//		TODO("Not yet implemented")

		for (i in 0 until this.viewList.size - 2 step 1) {
			val imageView = ImageView(this)
			if (i == 0) {
				imageView.setImageResource(R.drawable.image_select)
			} else {
				imageView.setImageResource(R.drawable.image)
			}
			this.linearLayout.addView(imageView, LinearLayout.LayoutParams(15, 15).apply {
				setMargins(5, 5, 5, 5)
			})
		}

	}

	private fun initView() {
		//分别将每一页要展示的View添加到集合库
		val view0 = View.inflate(this, R.layout.vp4, null)

		val view1 = View.inflate(this, R.layout.vp1, null)
		val view2 = View.inflate(this, R.layout.vp2, null)
		val view3 = View.inflate(this, R.layout.vp3, null)
		val view4 = View.inflate(this, R.layout.vp4, null)

		val view5 = View.inflate(this, R.layout.vp1, null)
		this.viewList.apply {
			add(view0)
			add(view1)
			add(view2)
			add(view3)
			add(view4)
			add(view5)
		}
	}

}